layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable"    //表格id
    };

    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        return [[
            {type: 'checkbox',fixed:'left'},
            {field: 'gcgWsjdId', hide: true, title: ''},
            {field: 'noticeId', align: "center", hide: true, sort: true, title: 'id'},
            {field: 'mingcheng', align: "center", sort: false, title: '门店名称(分店名称)',minWidth: 160,fixed:'left'},
            {field: 'dizhiXiangxi', align: "center", sort: false, title: '门店地址',minWidth: 200},
            {field: 'pinpai', align: "center", sort: false, title: '对外门店（品牌）',minWidth: 150},
            {field: 'mdTel', align: "center", sort: false,
                title:
                    '<div class="line-box flex"><span>门店（前厅/客服）电话<br/>订餐电话</span></div> ',
            },
            {field: 'pinlei', align: "center", sort: false, title: '菜系品类'},
            // {field: 'isTcw', align: "center", sort: false, title: '停车位'},
            // {field: 'renjunPrice', align: "center", sort: false, title: '人均价格',minWidth: 80},
            // {field: 'yhkName', align: "center", sort: false, title: '银行卡开户人',minWidth: 110},
            // {field: 'yhkHao', align: "center", sort: false, title: '银行卡卡号'},
            {field: 'yingyeZhizhao', align: "center", sort: false, title: '营业执照',templet: function (d) {
                    if (d.yingyeZhizhao === undefined || d.yingyeZhizhao === "") {
                        return "<p>暂无</p>";
                    } else {
                        return "<img src='"+ d.yingyeZhizhao + "' style='height: 100%; max-width: 100%;' onclick='imgViewer(this)' >" ;
                    }
                    // return "<img src='//www.baidu.com/img/pc_9bea003175fa9b942261f73533d83400.gif' style='height: 100%; max-width: 100%;' onclick='imgViewer(this)' >" ;
                }},
            // {field: 'shipinXkz', align: "center", sort: false, title: '食品经营许可证',minWidth: 130,templet: function (d) {
            //         if (d.shipinXkz === undefined || d.shipinXkz === "") {
            //             return "<p>暂无</p>";
            //         } else {
            //             return "<img src='"+ d.shipinXkz + "' style='height: 100%; max-width: 100%;' onclick='imgViewer(this)' >" ;
            //         }
            //         // return "<img src='https://wx1.sinaimg.cn/large/0024cZx9ly8gqlcz8gjf2j60f408iakb02.jpg' style='height: 100%; max-width: 100%;' onclick='imgViewer(this)' >" ;
            //     }},
            {field: 'zfbShouquan', align: "center", sort: false, title: '支付宝授权函',minWidth: 120,templet: function (d) {
                    if (d.zfbShouquan === undefined || d.zfbShouquan === "") {
                        return "<p>暂无</p>";
                    } else {
                        return "<img src='"+ d.zfbShouquan + "' style='height: 100%; max-width: 100%;' onclick='imgViewer(this)' >" ;
                    }
                    // return "<img src='https://wx4.sinaimg.cn/large/0024cZx9ly8gqlehp1d1zj60f408ijye02.jpg' style='height: 100%; max-width: 100%;' onclick='imgViewer(this)' >" ;
                }},
            // {field: 'mentouPics', align: "center", sort: false, title: '门头照',templet: function (d) {
            //         if (d.mentouPics === undefined || d.mentouPics === "") {
            //             return "<p>暂无</p>";
            //         } else {
            //             return "<img src='"+ d.mentouPics + "' style='height: 100%; max-width: 100%;' onclick='imgViewer(this)' >" ;
            //         }
            //         // return "<img src='https://wx1.sinaimg.cn/large/83255b1cly8gql7rszmavj20dw0dwwfo.jpg' style='height: 100%; max-width: 100%;' onclick='imgViewer(this)' >" ;
            //     }},
            // {field: 'neijingPics', align: "center", sort: false, title: '内景照',templet: function (d) {
            //         if (d.neijingPics === undefined || d.neijingPics === "") {
            //             return "<p>暂无</p>";
            //         } else {
            //             var time = new Array();
            //             var time =  d.neijingPics.split(',');
            //             var da1 = time[0];
            //             var da2 = time[1];
            //             return "<img src='"+ da1 + "' style='height: 100%; max-width: 100%;' onclick='imgViewer(this)'> <img src='"+ da2 + "' style='height: 100%; max-width: 100%;' onclick='imgViewer(this)'>" ;
            //         }
            //         // return "<img src='https://wx3.sinaimg.cn/large/001TVDbily8gqhwji0qc5j608q08qt9202.jpg' style='height: 100%; max-width: 100%;' onclick='imgViewer(this)' >" ;
            //     }},
            {field: 'createTime', align: "center", sort: false, title: '申请时间',minWidth: 150},
            {field: 'shenpiShijian', align: "center", sort: false, title: '通过(驳回)时间',minWidth: 150},
            {field: 'shStatus', align: "center", sort: false, title: '审核状态'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 120}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['mingcheng'] = $("#mingcheng").val();
        queryData['shStatus'] = $("#shStatus").val();
        queryData['yaoma'] = $("#yaoma").val();
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 批量下载按钮
     */
    var _delay = false;
    Notice.btnDown = function (type) {
        var checkRows = table.checkStatus(Notice.tableId);
        var data = checkRows.data;
        var ids = "";
        if (checkRows.data.length === 0) {
            Feng.error("请选择要批量下载的店面");
        } else {
            for (var i in data) {
                ids += data[i].gcgWsjdId + ",";
            }
            var operation = function () {
                if(_delay) {
                    return;
                };
                _delay = true;
                setTimeout(() =>{
                    _delay = false
                },2000);
                var ajax = new $ax(Feng.ctxPath + "/gcgWsjd/btnDown", function () {
                    $("#ways")[0].click();
                    Feng.success("下载成功!");
                    table.reload(Notice.tableId);
                }, function (result) {
                    Feng.error("下载失败!" + result.responseJSON.message + "!");
                });
                ajax.set("ids", ids);
                ajax.start();
            };
            Feng.confirm("是否批量下载?", operation);
        }
    };



    /**
     * 导出excel按钮
     */
    Notice.exportExcel = function () {
        var checkRows = table.checkStatus(Notice.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    form.on('select(startWorkYn)', function (data) {
        Notice.search();
    });



    /**
     * 点击编辑通知
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onEditNotice = function (data) {

        // window.location.href = Feng.ctxPath + '/gcgWsjd/reject?gcgWsjdId=' + data.gcgWsjdId;

        func.open({
            title: '支付宝审核驳回',
            content: Feng.ctxPath + '/gcgWsjd/reject?gcgWsjdId=' + data.gcgWsjdId,
            tableId: Notice.tableId
        });
    };

    /**
     * 审核通过
     * @param data
     */
    var _delay = false;
    Notice.tg = function (data) {
        var operation = function () {
            if(_delay) {
                return;
            };
            _delay = true;
            setTimeout(() =>{
                _delay = false
            },2000);
            var ajax = new $ax(Feng.ctxPath + "/gcgWsjd/zfbPass", function () {
                Feng.success("通过成功!");
                table.reload(Notice.tableId);
            }, function (result) {
                Feng.error("通过失败!" + result.responseJSON.message + "!");
            });
            ajax.set("ids", data.gcgWsjdId);
            ajax.start();
        };
        Feng.confirm("是否通过?", operation);
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Notice.tableId,
        url: Feng.ctxPath + '/gcgWsjd/zfbList',
        where: {shStatus:$('#shStatus').val()},
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: Notice.initColumn(),
        done:function (res) {
            $('.title-tr').remove()
            if (res.data == '' ){
                $('.layui-table-box').css('overflow','auto')
                $('.layui-table-header, .layui-table-body').css('overflow','visible')
            }
            $('.layui-table thead tr').before('<tr class="title-tr"><td></td><th colspan="5" style="text-align: center;height: 30px"><span class="jb">基本信息</span></th><th colspan="2" style="text-align: center;">证件信息</th><th colspan="4" class="th"></th></tr>')
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.search();
    });

    // 批量下载点击事件
    $('#btnDown').click(function () {
        Notice.btnDown();

    });

    // // 导出excel
    // $('#btnExp').click(function () {
    //     Notice.exportExcel();
    // });

    imgViewer = function (img) {
        let _tr = $(img).parents('tr')
        let index = $(_tr).index()

        // let $image = $('.layui-table-main tbody tr[data-index= '+ index + '] img')

        $('.layui-table-main tbody tr[data-index= '+ index + ']').viewer()
        return
    };

    // 工具条点击事件
    table.on('tool(' + Notice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Notice.onEditNotice(data);
        } else if (layEvent === 'passEdit') {
            Notice.tg(data);
        }
    });
});
